package com.gome.axwl.aoc.filter;

import com.anxl.sso.client.api.AnxlSsoApiImpl;
import com.anxl.sso.client.conf.AnxlSsoServerConfig;
import com.anxl.sso.client.filter.AnxlSsoFilter;
import com.gome.axwl.aoc.common.constants.LoginConstant;
import com.gome.axwl.aoc.service.sso.ServerGomeSsoClientService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsFileUploadSupport;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.MultipartConfigElement;

/**
 * @author chengh
 */
@Configuration
public class WebMvcConfigurer {

    @Autowired
    private ServerGomeSsoClientService anxlSsoClientService;

    @Autowired
    private AnxlSsoServerConfig anxlSsoServerConfig;

    @Autowired
    private AnxlSsoApiImpl anxlSsoApi;

    //上传文件配置
    @Bean(name = "multipartResolver")
    public CommonsFileUploadSupport commonsFileUploadSupport(){
        CommonsFileUploadSupport resolver = new CommonsMultipartResolver();
        resolver.setMaxInMemorySize(40960);
        resolver.setMaxUploadSize(10485760000L);
        return resolver;
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("100MB");
        factory.setMaxRequestSize("100MB");
        return factory.createMultipartConfig();
    }

    @Bean
    public AnxlSsoServerConfig anxlSsoServerConfig() {
        if (null == anxlSsoServerConfig) {
            AnxlSsoServerConfig anxlSsoServerConfig = new AnxlSsoServerConfig();
            return anxlSsoServerConfig;
        } else {
            return null;
        }
    }

    @Bean
    public AnxlSsoApiImpl anxlSsoApi() {
        if (anxlSsoApi == null) {
            AnxlSsoApiImpl anxlSsoApi = new AnxlSsoApiImpl(anxlSsoServerConfig.getServerUrl());
            return anxlSsoApi;
        } else {
            return null;
        }
    }

    @Bean
    public ServerGomeSsoClientService anxlSsoClientService() {
        if (anxlSsoClientService == null) {
            ServerGomeSsoClientService anxlSsoClientService = new ServerGomeSsoClientService();
            anxlSsoClientService.setAnxlSsoServerConfig(anxlSsoServerConfig);
            return anxlSsoClientService;
        } else {
            return null;
        }
    }

    /*
    @Bean
    public FilterRegistrationBean anxlSsoFilterRegistration() {
        if (null == anxlSsoServerConfig) {
            anxlSsoServerConfig();
        }
        if (null == anxlSsoApi) {
            anxlSsoApi();
        }

        if (null == anxlSsoClientService) {
            anxlSsoClientService();
        }
        anxlSsoClientService.setAnxlSsoServerConfig(anxlSsoServerConfig);
        AnxlSsoFilter anxlSsoFilter = new AnxlSsoFilter();
        anxlSsoFilter.setAnxlSsoApi(anxlSsoApi);
        anxlSsoFilter.setAnxlSsoServerConfig(anxlSsoServerConfig);
        anxlSsoFilter.setAnxlSsoClientService(anxlSsoClientService);
        FilterRegistrationBean registration = new FilterRegistrationBean(anxlSsoFilter);
        // 拦截规则
        registration.addUrlPatterns("/*");
        // 过滤api或资源
        // uri.contains(ex) || uri.endsWith(ex) || uri.equalsIgnoreCase(ex)
        registration.addInitParameter("exclude", "/auth/,api/index.html,index.html," + StringUtils.join(LoginConstant.SPRING_SSO_EXCLUDE_URI.toArray(), ','));
        // 过滤器名称
        registration.setName("anxlSsoFilter");
        // 是否自动注册 false 取消Filter的自动注册
        // registration.setEnabled(true);

        // 过滤器顺序
        registration.setOrder(1);

        return registration;
    }
*/


}